package com.bt.lms.ui.page.sys.style;

import com.bt.lms.ui.page.sys.vo.UserListInfo;
import javafx.event.EventHandler;
import javafx.scene.control.TableRow;
import javafx.scene.control.TableView;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundFill;
import javafx.scene.paint.Color;

/**
 * 列表行控制
 */
public class TableRowControl extends TableRow<UserListInfo> {

    private final TableRowControl tableRowControl = this;

    /**
     * 构建行控制
     * @param tableView 表格
     */
    public TableRowControl(TableView<UserListInfo> tableView) {

        super();

        //鼠标移入事件
        mouseMoved();

        //鼠标移出事件
        mouseExited();

    }

    /**
     * 鼠标移出事件实现
     */
    public void mouseExited() {

        //鼠标移出事件
        this.setOnMouseExited(new EventHandler<MouseEvent>() {

            /**
             * 事件实现
             * @param event the event which occurred
             */
            @Override
            public void handle(MouseEvent event) {

                //鼠标当移出的当行，奇数行白色，偶数行微黄色（第三个后）
                if (TableRowControl.this.getIndex() < 2) {
                    if (TableRowControl.this.getIndex() == 0)
                        oddNum();
                    else if (TableRowControl.this.getIndex() == 1)
                        evenNum();
                    else if (TableRowControl.this.getIndex() == 2)
                        oddNum();
                } else if (TableRowControl.this.getIndex() > 2) {
                    if (TableRowControl.this.getIndex() / 2 == 0)
                        oddNum();
                    else if (TableRowControl.this.getIndex() / 2 == 1)
                        evenNum();
                }

            }

            /**
             * 奇数行
             */
            private void oddNum() {
                BackgroundFill backgroundFill = new BackgroundFill(Color.rgb(255, 255, 255), null, null);
                Background background = new Background(backgroundFill);
                tableRowControl.setBackground(background);
            }

            /**
             * 偶数行
             */
            private void evenNum() {
                BackgroundFill backgroundFill = new BackgroundFill(Color.rgb(245, 245, 245), null, null);
                Background background = new Background(backgroundFill);
                tableRowControl.setBackground(background);
            }
        });
    }

    /**
     * 鼠标移入事件实现
     */
    public void mouseMoved() {
        //鼠标移入事件
        this.setOnMouseMoved(new EventHandler<MouseEvent>() {

            @Override
            public void handle(MouseEvent event) {
                //doSomething
                if (TableRowControl.this.getIndex() / 2 == 0) {
                    BackgroundFill backgroundFill = new BackgroundFill(Color.rgb(255, 253, 224), null, null);
                    Background background = new Background(backgroundFill);
                    tableRowControl.setBackground(background);
                }
            }
        });
    }

//this.setOnMouseClicked(new EventHandler<MouseEvent>() {
//        @Override
//        public void handle(MouseEvent event) {
//            //双击
//            if (event.getButton().equals(MouseButton.PRIMARY)
//                    && event.getClickCount() == 2
//                    && TableRowControl.this.getIndex() < tableView.getItems().size()) {
//                UserListInfo choosedStream = TableRowControl.this.getItem();//获取点击的对象
//                int choosedIndex = TableRowControl.this.getIndex();//获取点击的index，就是表上的第几项
//                System.out.println(choosedStream);
//                System.out.println(choosedIndex);
//                //业务代码。。。
//
//            }
//            //单击
//            if (event.getButton().equals(MouseButton.PRIMARY)
//                    && event.getClickCount() == 1
//                    && TableRowControl.this.getIndex() < tableView.getItems().size()) {
//                UserListInfo choosedStream = TableRowControl.this.getItem();//获取点击的对象
//                int choosedIndex = TableRowControl.this.getIndex();//获取点击的index，就是表上的第几项
//                System.out.println(choosedStream);
//                System.out.println(choosedIndex);
//                //业务代码。。。
//
//            }
//        }
//    });
    public TableRowControl() {

    }





}

